package com.walker.routing;

import com.rabbitmq.client.*;
import com.walker.BaseConfig;

/**
 * @Description:
 * @ProjectName: walker-step
 * @Package: com.walker.routing
 * @ClassName: RoutingRecv02
 * @Author: yangyl
 * @CreateDate: 2020/6/21 14:31
 */
public class RoutingRecv02 {


    public static final String EXCHANGE = "waring.exchange.02";
    public static final String QUEUENAME = "waring.queue.04";
    public static final String ROUTINGKEY = "waring.green";

    public static ConnectionFactory connectionFactory() {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(BaseConfig.HOST);
        connectionFactory.setVirtualHost(BaseConfig.VHOST);
        connectionFactory.setPort(5672);
        connectionFactory.setUsername(BaseConfig.USERNAME);
        connectionFactory.setPassword(BaseConfig.PASSWORD);
        return connectionFactory;
    }

    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = connectionFactory();
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(QUEUENAME,true,false,false,null);
        channel.queueBind(QUEUENAME,EXCHANGE,ROUTINGKEY);

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume(QUEUENAME, true, deliverCallback, consumerTag -> {
        });

    }
}
